package com.stoneframe.appserver.controller;

import java.util.List;

import javax.annotation.Resource;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stoneframe.appserver.service.SysUserService;
import com.stoneframe.base.controller.BaseGenericController;
import com.stoneframe.base.service.BaseService;
import com.stoneframe.base.util.AppHelper;
import com.stoneframe.base.util.PageData;
import com.stoneframe.base.util.ToolHelper;


@Controller
@RequestMapping(value="sysUser")
public class SysUserController extends BaseGenericController {
	
	@Resource(name="sysUserService")
	private SysUserService sysUserService;

	@Override
	public BaseService getService() {
		// TODO Auto-generated method stub
		return sysUserService;
	}
	
	/**
	 * 下拉选项
	 * */
	@RequestMapping(value="comboBox")
	@ResponseBody
	public Object comboBox(){
		logBefore(logger, className+"comboBox");

		PageData pd=this.getPageData();
		List<PageData> list=null;
		try{
			list=sysUserService.listAll(pd);
			logAfter(logger);
			return AppHelper.returnObject(pd,list);
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}
		
	}
	
	/**
	 * 更新记录
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	@Override
	public Object edit() {
		// TODO Auto-generated method stub
		logBefore(logger, className+".edit");
		
		PageData pd=this.getPageData();
		
		try{
			if(ToolHelper.notEmpty(pd.getString("keyValue"))){
				getService().edit(pd);
			}else{
				pd.put("keyValue", this.get32Guid());
				pd.put("userPwd", new SimpleHash("SHA-1",pd.getString("userName"),pd.getString("userPwd")).toString());
				getService().save(pd);
			}
			return AppHelper.successObject("操作成功！");
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject("操作失败！"+e.getMessage());
		}
	}

	/**
	 * 修改密码
	 * */
	@ResponseBody
	@RequestMapping(value="editUserPwd")
	public Object editUserPwd(){
		logBefore(logger, className+"editUserPwd");
		
		PageData pd=this.getPageData();
		try{
			pd.put("userPwd", new SimpleHash("SHA-1",pd.getString("userName"),pd.getString("userPwd")).toString());
			sysUserService.editUserPwd(pd);
			logAfter(logger);
			return AppHelper.successObject("修改成功");
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}

}
